﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace ScrumSim
{
    public class Enemy : AnimatedGameObject
    {
        private GameObject mPlatform;
        private bool mMoving;
        private int mHDelta;

        public bool dead;

        public Enemy(Game1 pGame, Texture2D pIdleTex, Texture2D pMoveTex, GameObject pPlatform, bool pMoving)
            :base(pGame, "RedRect")
        {
            mPlatform = pPlatform;
            mMoving = pMoving;
            mHDelta = 100;

            if(mMoving)
                AddAnimation("only", 0.5f, 2, pMoveTex, new Rectangle(0, 0, 32, 32));
            else
                AddAnimation("only", 0.5f, 2, pIdleTex, new Rectangle(0, 0, 32, 32));

            mBoundingBox = new Rectangle(0, 0, 32, 32);

            Play("only");

            dead = false;
        }

        public void die()
        {
            dead = true;
            mFlipY = true;
        }

        public override void Update(GameTime gameTime)
        {
            mFlipX = mHDelta > 0;

            if (dead)
            {
                mPosition.Y += 500 * (float)gameTime.ElapsedGameTime.TotalSeconds;
            }
            else if (mMoving)
            {
                mPosition.X += (int)(mHDelta * gameTime.ElapsedGameTime.TotalSeconds);
                if (mPosition.X + mBoundingBox.Width > mPlatform.position.X + mPlatform.BBox.Width)
                {
                    mPosition.X = mPlatform.position.X + mPlatform.BBox.Width - mBoundingBox.Width;
                    mHDelta = -mHDelta;
                }
                else if (mPosition.X < mPlatform.position.X)
                {
                    mPosition.X = mPlatform.position.X;
                    mHDelta = -mHDelta;
                }
            }

            base.Update(gameTime);
        }
    }
}
